/*
 * @Description:
 * @Date: 2022-06-21 11:34:19
 * @LastEditTime: 2023-12-18 20:54:02
 * @FilePath: \vue3\src\router\index.js
 */
import { createRouter, createWebHistory } from "vue-router";

// 引入组件

import computedTest from "../views/vue3Study/computedTest.vue";
import setupTest from "../views/vue3Study/setupTest.vue";
import onErrorCapturedTest from "../views/vue3Study/onErrorCapturedTest.vue";
import refTest from "../views/vue3Study/refTest.vue";
import reactiveTest from "../views/vue3Study/reactiveTest.vue";
import toTest from "../views/vue3Study/toTest.vue";
import watchTest from "../views/vue3Study/watchTest.vue";
import piniaTest from "../views/vue3Study/piniaTest.vue";
import customInstruction from "../views/vue3Study/customInstruction.vue";
import css from "../views/vue3Study/css.vue";
import loading from "../views/vue3Study/loading.vue";
import pagination from "../views/vue3Study/pagination.vue";
import mock from "../views/mock/mock.vue";
import gaodeMap from "../views/map/gaodeMap.vue";
import student from "../views/nestjs/student/index.vue";
import score from "../views/nestjs/score/index.vue";
import login from "../views/login/index.vue";
import home from "../views/home.vue";
// import anime from "../views/tripartitelibrary/anime/index.vue";
import three from "../views/tripartitelibrary/three/index.vue";
import studyMap from "../views/map/index.vue";
import swiper from "../views/tripartitelibrary/swiper/index.vue";
import canvas from "../views/css/canvas.vue";
import ExcelDetail from "../views/tripartitelibrary/excelTest/components/ExcelDetail.vue";
import autoRoutes from "./autoRoutes.ts";
import myphonecost from "../views/myphonecost/index.vue";
import { getToken, setToken } from "@/utils/auth";

const routes = [
  {
    path: "/mycomputed",
    component: computedTest,
    name: "computedTest",
  },
  {
    path: "/setupTest",
    component: setupTest,
    name: "setupTest",
  },
  {
    path: "/onErrorCapturedTest",
    component: onErrorCapturedTest,
    name: "onErrorCapturedTest",
  },
  {
    path: "/refTest",
    component: refTest,
    name: "refTest",
  },
  {
    path: "/reactiveTest",
    component: reactiveTest,
    name: "reactiveTest",
  },
  {
    path: "/toTest",
    component: toTest,
    name: "toTest",
  },
  {
    path: "/watchTest",
    component: watchTest,
    name: "watchTest",
  },
  {
    path: "/piniaTest",
    component: piniaTest,
    name: "piniaTest",
  },
  {
    path: "/customInstruction",
    component: customInstruction,
    name: "customInstruction",
  },
  {
    path: "/mock",
    component: mock,
    name: "mock",
  },
  {
    path: "/gaodeMap",
    component: gaodeMap,
    name: "gaodeMap",
  },
  {
    path: "/studyMap",
    component: studyMap,
    name: "studyMap",
  },
  {
    path: "/css",
    component: css,
    name: "css",
  },
  {
    path: "/loading",
    component: loading,
    name: "loading",
  },
  {
    path: "/pagination",
    component: pagination,
    name: "pagination",
  },
  {
    path: "/nestjs/student",
    component: student,
    name: "student",
  },
  {
    path: `/nestjs/score/:id`,
    component: score,
    name: "score",
  },
  // {
  //   path: "/login",
  //   component: login,
  //   name: "login",
  // },
  // {
  //   path: "/anime",
  //   component: anime,
  //   name: "anime",
  // },
  {
    path: "/three",
    component: three,
    name: "three",
  },
  {
    path: "/swiper",
    component: swiper,
    name: "swiper",
  },
  {
    path: "/canvas",
    component: canvas,
    name: "canvas",
  },

];

const defaultRoute = [
  {
    path: "/",
    redirect: "/home",
    // children: [...autoRoutes],
  },
  {
    path: "/home",
    component: home,
    name: "home",
  },
  {
    path: "/login",
    component: login,
    name: "login",
  },
  {
    path: "/ExcelDetail",
    component: ExcelDetail,
    name: "ExcelDetail",
  },
];
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHistory(),
  routes: [...defaultRoute], // `routes: routes` 的缩写
  scrollBehavior: () => ({ left: 0, top: 0 }),
});
// console.log("autoRoutes:", autoRoutes);
router.beforeEach(async (to, from, next) => {
  //白名单直接进入
  if (to.path === "/login") {
    return next();
  }
  //登录用户才可以
  // if (!getToken()) {
  //   return next({ path: "/login", replace: true });
  // }
  if (router.getRoutes().length < 5) {
    autoRoutes();
    return next({ ...to, replace: true });
  }
  next()
});
export default router;
